﻿<template>
    <full-screen-dialog
        :title="titleMap[mode]"
        v-model="visible"
        :width="1300"
        destroy-on-close
        @closed="$emit('closed')"
    >
        <el-form :model="entityObj" :rules="rules" :disabled="mode == 'Show'" ref="dialogForm">
            <el-row>
                <el-col :span="16">
                    <el-form-item label-width="155px" label="还货订单号:">
                        <el-tooltip
                            placement="top"
                            v-for="(item,index) in entityObj.orderDetail"
                            :key="item.backGoodsOrderId"
                        >
                            <template #content>
                                还货单位: {{item.purchaseUnityName}}
                                <br />
                                申请人:{{item.backGoodsName}}
                                <br />
                                申请时间:{{item.backGoodsDate}}
                                <br />
                                收货状态:{{item.taskStatusDesc}}
                            </template>
                            <span
                                style="color: blue;margin-right: 5px;"
                            >{{item.backGoodsOrderCode}}{{ index !== entityObj.orderDetail.length - 1 ? ',' : '' }}</span>
                            <el-button size="small" type="primary" @click="opSendDetail()">发货明细</el-button>
                        </el-tooltip>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label-width="155px" label="收货人:" prop="taskName">
                        <el-input v-model="entityObj.taskName" />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label-width="155px" label="入库时间:" prop="taskDate">
                        <el-input v-model="entityObj.taskDate" />
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="24">
                    <el-form-item label-width="155px" label="收货清单:" prop="taskBackGoodsOrderItems">
                        <el-form inline label-width="49px" style="margin-top: 5px">
                            <el-form-item >
                                <el-input
                                    placeholder="---请输入物料编号---"
                                    v-model="queryList.materialCode"
                                    style="width: 160px"
                                />
                            </el-form-item>
                            <el-form-item >
                                <el-input
                                    placeholder="---请输入物料名称---"
                                    v-model="queryList.materialName"
                                    style="width: 160px"
                                />
                            </el-form-item>
                            <el-form-item >
                                <el-tree-select
                                    v-model="queryList.materialTypeIds"
                                    placeholder="---请选择物料分类---"
                                    collapse-tags
                                    collapse-tags-tooltip
                                    :props="{ label: 'label', value: 'value', children: 'children' }"
                                    :data="materialTypeTree"
                                    multiple
                                    :render-after-expand="false"
                                    show-checkbox
                                    style="width: 160px"
                                />
                            </el-form-item>
                            <el-button
                                :icon="Search"
                                 
                                type="primary"
                                @click="handleSearch"
                            >查询</el-button>
                            <el-button :icon="Search" type="info" plain @click="resetSearch">重置</el-button>
                            <el-form inline label-width="49px" style="margin-top: 5px"></el-form>
                        </el-form>
                        <el-table
                            :data="itemsData"
                            border
                            style="width: 100%"
                            show-summary
                        >
                            <el-table-column label="物料编码" width="110" prop="materialCode"></el-table-column>
                            <el-table-column label="物料名称" width="160" prop="materialName"></el-table-column>
                            <el-table-column label="分类" width="100" prop="materialTypeName"></el-table-column>
                            <el-table-column label="规格" width="120" prop="materialSpecs"></el-table-column>
                            <el-table-column label="单位" width="70" prop="unitName"></el-table-column>
                            
                            <!-- <el-table-column label="待收货数量" width="100" prop="pendingTaskQty"></el-table-column> -->
                            <el-table-column label="入库仓库" prop="warehouseId" min-width="120">
                                <template #default="scope">
                                    <el-select
                                        placeholder="---请选择出库仓库 ---"
                                        v-model="scope.row.warehouseId"
                                        :disabled="scope.row.taskQty==0"
                                        style="width: 100%"
                                    >
                                        <el-option label="---请选择---" value />
                                        <el-option
                                            v-for="item in scope.row.warehouses"
                                            :key="item.warehouseId"
                                            :label="item.warehouseName"
                                            :value="item.warehouseId"
                                        />
                                    </el-select>
                                </template>
                            </el-table-column>
                            <el-table-column label="入库数量" min-width="120">
                                <template #default="scope">
                                    <el-input-number
                                        v-model="scope.row.taskQty"
                                        style="width: 100%"
                                        :precision="0"
                                        :step="1"
                                        :disabled="scope.row.taskQty==0"
                                        controls-position="right"
                                    ></el-input-number>
                                </template>
                            </el-table-column>
                            <el-table-column label="还货数量" width="100" prop="qty"></el-table-column>
                            <el-table-column label="发货数量" width="100" prop="totalSendQty"></el-table-column>
                            <el-table-column label="累计收货" width="100" prop="totalTaskQty"></el-table-column>
                        </el-table>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label-width="120px" label="收货图片">
                        <sc-upload-multiple
                            v-model="entityObj.taskFileStr"
                            :objName="'Supply/TaskFiles'"
                            draggable
                            :uploadType="'OSS'"
                            :bucketName="'jtmc'"
                            :limit="8"
                            tip="最多上传8个文件,请上传图像格式文件"
                        />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
            <el-button
                v-if="mode != 'show'"
                type="primary"
                :loading="isSaveing"
                @click="isSaveing=true;submit()"
            >确认收货入库</el-button>
        </template>
    </full-screen-dialog>

    <send-detail-dialog
        v-if="dialogSendDetail"
        ref="sendDetailDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogSendDetail = false"
    ></send-detail-dialog>
</template>



<script>
import tool from "@/utils/tool";
import sendDetailDialog from "./sendDetail";
export default {
    emits: ["success", "closed"],
    components: {
        sendDetailDialog,
    },
    data() {
        return {
            mode: "Task",
            titleMap: {
                Send: "收货",
                Task: "收货",
            },
            entityObj: {
                orderDetail: [],
            },
            materialState: false,
            queryList: {},
            visible: false,
            isSaveing: false,
            needSaveFlag: false,
            dialogSendDetail: false,
            //验证规则
            rules: {
                taskName: [
                    { required: true, type: "string", message: "请填写" },
                ],
                taskDate: [
                    { required: true, type: "string", message: "请填写" },
                ],
                taskTrusteeshipOrderItems: [
                    { required: true, type: "array", message: "请填写" },
                ],
            },
            materialTypeTree: [],
            optionsSuppliers: [],
            itemsData: [],
        };
    },
    computed: {
        // itemsData() {
        //     let filtered = this.entityObj.purchaseProtocolItems;
        //     if (this.queryList.materialTypeIds&&this.queryList.materialTypeIds.length>0) {
        //         filtered = filtered.filter(item => {
        //             return this.queryList.materialTypeIds.includes(item.materialTypeId);
        //         });
        //     }
        //     if (this.queryList.materialCode) {
        //         filtered = filtered.filter(item => {
        //             return item.materialCode.includes(this.queryList.materialCode);
        //         });
        //     }
        //     if (this.queryList.materialName) {
        //         filtered = filtered.filter(item => {
        //             return item.materialName.includes(this.queryList.materialName);
        //         });
        //     }
        //     return filtered;
        // },
    },
    methods: {
        //显示=
        open(mode = "Add") {
            this.mode = mode;
            this.visible = true;
            return this;
        },
        //发货明细
        opSendDetail() {
            if (
                !this.entityObj.sendOrderItems ||
                this.entityObj.sendOrderItems.length == 0
            ) {
                this.$message.error("无发货记录");
                return;
            }
            const sendItems = this.entityObj.sendOrderItems.filter(
                (p) => p.supplierId == this.entityObj.supplierId
            );
            this.dialogSendDetail = true;
            this.$nextTick(() => {
                this.$refs.sendDetailDialog.setData(sendItems);
                this.$refs.sendDetailDialog.open("Send");
            });
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    try {
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                "back-goods-order/batch-task",
                                this.entityObj
                            );
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$emit("fatherMethod");
                            this.isSaveing = false;
                            this.visible = false;
                        } else {
                            this.$message.error("操作失败:" + returnValue.msg);
                            this.isSaveing = false;
                        }
                    } catch (error) {
                        this.loading = false;
                        this.isSaveing = false;
                        return false;
                    }
                }
            });
        },
        //表单注入数据
        async setData(data) {
            try {
                var backGoodsOrderIds= data.map(p=>p.backGoodsOrderId);
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    `back-goods-order/wait-task-items`,
                    {backGoodsOrderIds:backGoodsOrderIds}
                );
                if (returnValue.status == 200) {
                    this.entityObj = {
                        orderDetail: [...data],
                        taskBackGoodsOrderItems:[...returnValue.response],
                        taskDate: tool.formatDate(new Date()),
                        taskName: tool.data.get("USER_INFO").Name,
                    };
                    this.queryList = {};
                    this.handleSearch();
                    console.log("this.entityObj", this.entityObj);
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "material-type/tree",
                    { status: 1 }
                );
                if (returnValue.status == 200) {
                    this.materialTypeTree = returnValue.response;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        handleSearch() {
            this.itemsData = this.entityObj.taskBackGoodsOrderItems;
            if (
                this.queryList.materialTypeIds &&
                this.queryList.materialTypeIds.length > 0
            ) {
                this.itemsData = this.itemsData.filter((item) => {
                    return this.queryList.materialTypeIds.includes(
                        item.materialTypeId
                    );
                });
            }

            this.itemsData = this.itemsData.filter(
                (item) => item.supplierId == this.entityObj.supplierId
            );

            if (this.queryList.materialCode) {
                this.itemsData = this.itemsData.filter((item) => {
                    return item.materialCode.includes(
                        this.queryList.materialCode
                    );
                });
            }

            if (this.queryList.materialName) {
                this.itemsData = this.itemsData.filter((item) => {
                    return item.materialName.includes(
                        this.queryList.materialName
                    );
                });
            }
            console.log(this.entityObj.purchaseProtocolItems);
        },
        // getSummaries(param) {
        //     const { columns, data } = param;
        //     const sums = [];
        //     columns.forEach((column, index) => {
        //         if (index === 0) {
        //             sums[index] = "合计";
        //             return;
        //         }
        //         const values = data.map((item) =>
        //             Number(item[column.property])
        //         );

        //         if (
        //             column.property == "deliveryQty" ||
        //             column.property == "taskQty"
        //         ) {
        //             if (!values.every((item) => isNaN(item))) {
        //                 const total = values.reduce((a, b) => a + b, 0);
        //                 sums[index] = total;
        //             }
        //         }
        //     });
        //     return sums;
        // },
    },
    async mounted() {
        await this.initOptions();
    },
};
</script>

<style scoped>
.el-form-item {
    margin-bottom: 10px !important;
}
</style>
